/**
 *
 * @type {{methods: {chartToolbox(): {feature: {}, show: boolean}, chartTooltip(): {padding: *, borderColor: string, backgroundColor: string, borderWidth: number, show: *|boolean, trigger: string, textStyle: {color: string, fontSize: number, fontWeight: string}}, chartYlineStyle(*=, *=): void, chartLegend(*, *=): {orient: chartStyle.methods.config.styles.legendConfig.orient, top: chartStyle.methods.config.styles.legendConfig.top, left: chartStyle.methods.config.styles.legendConfig.left, bottom: chartStyle.methods.config.styles.legendConfig.bottom, show: boolean|*, icon: chartStyle.methods.config.styles.legendConfig.icon, right: chartStyle.methods.config.styles.legendConfig.right, textStyle: {color: chartStyle.methods.config.styles.legendConfig.color}, type: chartStyle.methods.config.styles.legendConfig.type}, filletedCorner(*): ([]|undefined), chartXlineStyle(*=): void, chartGrid(): {top: number, left: number, bottom: number, right: number}}}}
 */
const chartStyle = {
    methods: {
        i18n(name, text, key) {
            return this.$ti18(name, text, "previewLang", key);
        },
        //工具栏目配置
        chartToolbox() {
            let toolbox = {
                show: true,
                feature: {}
            }
            if (this.config.styles.toolbarConfig) {
                if (this.config.styles.toolbarConfig.restoreBtn) {
                    //还原按钮
                    toolbox.feature['restore'] = {
                        title: this.i18n("restore","还原")
                    };
                }
                if (this.config.styles.toolbarConfig.downloadBtn) {
                    //下载按钮
                    toolbox.feature['saveAsImage'] = {
                        title: this.i18n("saveAsImage","保存为图片")
                    };
                }
                if (this.config.styles.toolbarConfig.dataView) {
                    let dataView = {
                        readOnly: false,
                        title:this.i18n("dataView","数据视图")
                    };
                    toolbox.feature['dataView'] = dataView;//数据分析按钮
                }
                if (this.config.styles.toolbarConfig.dataScaling) {
                    let dataZoom = {
                        yAxisIndex: "none",
                        title:{
                            zoom: this.i18n("zoom","区域缩放"),
                            back: this.i18n("back","区域缩放还原"),
                        }
                    };
                    toolbox.feature['dataZoom'] = dataZoom;//区域缩放
                }
                if (this.config.styles.toolbarConfig.switchBtn) {
                    let magicType = {
                        type: ["line", "bar"],
                        title:{
                            line: this.i18n("switchLine","切换为折线图"),
                            bar: this.i18n("switchBar","切换为柱状图")
                        }
                    }
                    toolbox.feature['magicType'] = magicType;//柱状图和线切换
                }
                if (this.config.styles.toolbarConfig.suffix == 'px') {
                    toolbox['right'] = this.config.styles.toolbarConfig.fromRight;
                } else if (this.config.styles.toolbarConfig.suffix == '%') {
                    let fromRight = this.config.styles.toolbarConfig.fromRight + '%';
                    toolbox['right'] = fromRight;
                }
            }
            return toolbox
        },
        //grid离容器的边距设置
        chartGrid() {
            let grid = {
                top: 0,
                left: 0,
                bottom: 0,
                right: 0,
            }
            if (this.config.styles.gridConfig) {
                if (this.config.styles.gridConfig.topSuffix == 'px') {
                    grid.top = this.config.styles.gridConfig.top;
                } else {
                    grid.top = this.config.styles.gridConfig.top + '%';
                }
                if (this.config.styles.gridConfig.rightSuffix == 'px') {
                    grid.right = this.config.styles.gridConfig.right;
                } else {
                    grid.right = this.config.styles.gridConfig.right + '%';
                }
                if (this.config.styles.gridConfig.bottomSuffix == 'px') {
                    grid.bottom = this.config.styles.gridConfig.bottom;
                } else {
                    grid.bottom = this.config.styles.gridConfig.bottom + '%';
                }
                if (this.config.styles.gridConfig.leftSuffix == 'px') {
                    grid.left = this.config.styles.gridConfig.left;
                } else {
                    grid.left = this.config.styles.gridConfig.left + '%';
                }
            }
            return grid
        },
        //提示框的样式修改
        chartTooltip() {
            let tooltipConfig = this.config.styles.tooltipConfig;
            if(!tooltipConfig){
                return {};
            }
            let tooltip = {
                show: this.config.styles ? this.config.styles.tooltipShow : false,
                trigger: 'axis',
                borderColor: tooltipConfig.borderColor,
                backgroundColor: tooltipConfig.raBgColor,
                borderWidth: tooltipConfig.borderWidth,
                padding: this.filletedCorner(tooltipConfig.padding),
                textStyle: {
                    fontSize: tooltipConfig.fontSize,
                    color: tooltipConfig.fontColor,
                    fontWeight: tooltipConfig.fontStyle,
                }
            };
            if (tooltipConfig.formatter && tooltipConfig.formatter.replace(/\s*/g, "")) {
                if (tooltipConfig.formatterMode == "template") {
                    tooltip.formatter = tooltipConfig.formatter;
                } else {
                    //格式化
                    tooltip.formatter = new Function("params", "ticket", "callback", tooltipConfig.formatter);//格式化
                }
            }
            return tooltip
        },
        //圆角数据格式化
        filletedCorner(radiusList) {
            let radius = radiusList.split(',')
            if (radius.length == 4) {
                let numberList = [];
                radius.filter(res => {
                    numberList.push(Number(res))
                })
                return numberList
            }
        },
        //Y轴样式的修改
        chartYlineStyle(yAxis, yAxisConfig,measures) {
            if (!yAxisConfig) {
                yAxisConfig = this.config.styles.yAxisConfig;
            }
            if (!measures) {
                measures =this.config.dataSources.measures;
            }
            let YlineStyle = {
                color: yAxisConfig?yAxisConfig.axisColor:[]
            }
            yAxis["axisLine"] = {};
            //是否显示y轴的线
            if (yAxisConfig && yAxisConfig.yAxisShow) {
                yAxis.axisLine['show'] = yAxisConfig.yAxisShow;
                yAxis.axisLine['lineStyle'] = YlineStyle;
            }
            //对Y轴样式的修改字体颜色大小加粗
            if (yAxis && yAxisConfig && yAxisConfig.fontSize) {
                let axisLabelY = {};
                axisLabelY.fontWeight = yAxisConfig.fontStyle;//字体加粗
                axisLabelY.fontSize = yAxisConfig.fontSize;//字体大小
                axisLabelY.color = yAxisConfig.fontColor;//字体颜色
                axisLabelY.rotate = yAxisConfig.rotationAngle;//旋转角度
                if (yAxisConfig.formatter && yAxisConfig.formatter.replace(/\s*/g, "")) {
                    if (yAxisConfig.formatterMode == "template") {
                        axisLabelY.formatter = yAxisConfig.formatter;
                    } else {
                        //格式化
                        axisLabelY.formatter = new Function("value", "index", yAxisConfig.formatter);
                    }
                }
                yAxis["axisLabel"] = axisLabelY;
            }
            //最大最小
            yAxis.min = yAxisConfig && yAxisConfig.min ? Number(yAxisConfig.min) : undefined;
            yAxis.max = yAxisConfig && yAxisConfig.max ? Number(yAxisConfig.max) : undefined;
            //间隔
            yAxis.interval = yAxisConfig && yAxisConfig.interval ? Number(yAxisConfig.interval) : undefined;
            //获取y名称
            if(yAxisConfig && yAxisConfig.showName && measures){
                let title="";
                for (let i = 0; i < measures.length; i++) {
                    if(title){
                        title+="-";
                    }
                    title+=measures[i].fieldLabel;
                }
                yAxis["name"]=title;
                yAxis["nameLocation"]=yAxisConfig.nameLocation || "end";
            }
        },
        //Y轴在grid区域中的分隔线
        splitLineStyle(yAxis, yGrid) {
            if (!yGrid) {
                yGrid = this.config.styles.yGrid
            }
            yAxis['splitLine'] = {};
            let splitLine = {
                show: true,
                lineStyle: {}
            }
            if (yGrid) {
                splitLine.show = this.config.styles.yGridShow;//展示y
                splitLine.lineStyle['color'] = yGrid.borderColor;//y颜色
                splitLine.lineStyle['type'] = yGrid.lineStyleType;//y
            }
            yAxis['splitLine'] = splitLine;
        },
        //x轴样式的修改
        chartXlineStyle(xAxis) {
            let xAxisConfig = this.config.styles.xAxisConfig;
            let lineStyle = {
                color: xAxisConfig && xAxisConfig.axisColor? xAxisConfig.axisColor : 'rgba(66,133,244,0.4)'
            }
            xAxis["axisLine"] = {};
            xAxis.axisLine['lineStyle'] = lineStyle;
            //对x轴样式的修改字体颜色大小加粗
            if (xAxisConfig && xAxisConfig.fontSize) {
                let axisLabel = {};
                axisLabel.fontWeight = xAxisConfig.fontStyle;//字体加粗
                axisLabel.fontSize = xAxisConfig.fontSize;//字体大小
                axisLabel.color = xAxisConfig.fontColor;//字体颜色
                axisLabel.rotate = xAxisConfig.rotationAngle;//旋转角度
                if (xAxisConfig.formatter && xAxisConfig.formatter.replace(/\s*/g, "")) {
                    if (xAxisConfig.formatterMode == "template") {
                        axisLabel.formatter = xAxisConfig.formatter;
                    } else {
                        //格式化
                        axisLabel.formatter = new Function("value", "index", xAxisConfig.formatter);
                    }
                }
                xAxis['axisLabel'] = axisLabel;
            }
            //获取x名称
            if(xAxisConfig && xAxisConfig.showName){
                let title="";
                for (let i = 0; i < this.config.dataSources.dimensions.length; i++) {
                    if(title){
                        title+="-";
                    }
                    title+=this.config.dataSources.dimensions[i].fieldLabel;
                }
                xAxis["name"]=title;
                xAxis["nameLocation"]=xAxisConfig.nameLocation || "end";
            }
        },
        /**
         * 图列的配置
         * @param legend 图例组件
         * @param notData 不传入data
         */
        chartLegend(legend, notData) {
            let legendConfig = this.config.styles.legendConfig;
            if(!legendConfig){
                return {};
            }
            let legendData = {
                type: legendConfig.type,
                show: this.config.styles.legendShow,
                icon: legendConfig.icon,
                orient: legendConfig.orient,
                textStyle: {
                    color: legendConfig.color
                }
            }
            if (legendConfig.position == 'top') {
                legendData["y"] = 'top';
                if (legendConfig.textAlign) {
                    legendData["x"] = legendConfig.textAlign;
                }
            } else if (legendConfig.position == 'bottom') {
                legendData['y'] = "bottom";
                if (legendConfig.textAlign) {
                    legendData['x'] = legendConfig.textAlign;
                }
            } else if (legendConfig.position == 'left') {
                legendData['x'] = 'left';
                if (legendConfig.textAlign) {
                    legendData['y'] = legendConfig.textAlign;
                }
            }else if (legendConfig.position == 'right') {
                legendData['x'] = 'right';
                if (legendConfig.textAlign) {
                    legendData['y']=legendConfig.textAlign;
                }
            }
            if (!notData) {
                legendData.data = legend
            }
            return legendData
        },
        //获取预警线数据和样式
        markLine() {
            let list = [];
            if (this.config.advancedConf.forewarningConfig) {
                let yAxis = 0
                this.config.advancedConf.forewarningConfig.filter(res => {
                    if (res.type == 'average') {
                        yAxis = this.average(res.average)
                    } else {
                        yAxis = res.immobilizationNumber
                    }
                    let obj = {
                        name: res.name,
                        lineStyle: {
                            normal: {
                                type: res.lineType, //dashed为虚线，solid为实线
                                color: res.lineColor, // 这儿设置安全基线颜色
                                width: res.lineWidth,
                            }
                        },
                        label: {
                            formatter: res.name,
                            color: res.lineColor
                        },
                    };
                    //安全基线 1
                    if (this.config.chartType == "horizontalBar") {
                        obj['xAxis'] = yAxis
                    } else {
                        obj['yAxis'] = yAxis
                    }
                    list.push(obj)
                })
                return list
            }
        },
        average(name) {
            let average = 0;
            this.data.filter(res => {
                average = average + res[name]
            })
            return parseInt(average / this.data.length);
        }

    }

};
export default chartStyle
